package io.netty.myExample.zhanglong.p1启动一个服务;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author : duyutian
 * @Date : 2019/11/19 13:29
 * @Version 1.0
 * netty开始的地方 创建http连接
 */
@Slf4j
public class NettyStartService {
    public static void main(String[] args) {
        /**
         * 构建一对线程组
         */
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();
        try {

            /**
             * netty服务器创建 构建启动类
             */
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new TestInitializer());
            /**
             * 启动 ，同事启动方式为同步
             */
            ChannelFuture channelFuture = serverBootstrap
                    .bind(8088).sync();
            /**
             * 监听关闭的同步？
             */
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            /**
             * 发生错误
             */
            log.error("netty执行发生错误", e);
        } finally {
            //优雅关闭
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }


    }
}
